package com.example.accountingapp;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 备忘录
 */
public class NotepadActivity extends AppCompatActivity {
    MySqliteOpenHelper helper = null;
    private Activity myActivity;
    private RecyclerView recyclerView;//垃日记列表
    private NotepadAdapter notepadAdapter;//日记适配器
    private List<Notepad> list;//列表
    private LinearLayout llEmpty;
    private EditText etQuery;
    private ImageView ivSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myActivity = this;
        setContentView(R.layout.activity_notepad);
        helper = new MySqliteOpenHelper(myActivity);
        recyclerView = findViewById(R.id.rv_list);
        llEmpty = findViewById(R.id.ll_empty);
        etQuery = findViewById(R.id.et_query);
        ivSearch = findViewById(R.id.iv_search);
        //=1.1、创建布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(myActivity);//三列
        recyclerView.setLayoutManager(layoutManager);
        //==2、实例化适配器
        //=2.1、初始化适配器
        notepadAdapter = new NotepadAdapter();
        //=2.3、设置recyclerView的适配器
        recyclerView.setAdapter(notepadAdapter);
        loadData();
        notepadAdapter.setItemListener(new NotepadAdapter.ItemListener() {
            @Override
            public void ItemClick(Notepad notepad) {
                Intent intent = new Intent(myActivity, AddNotepadActivity.class);
                intent.putExtra("notepad", notepad);
                startActivity(intent);
            }

            @Override
            public void ItemLongClick(Notepad notepad) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(myActivity);
                dialog.setMessage("确认要删除该数据吗");
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SQLiteDatabase db = helper.getWritableDatabase();
                        if (db.isOpen()) {
                            db.execSQL("delete from notepad where id = " + notepad.getId());
                            db.close();
                        }
                        Toast.makeText(myActivity, "删除成功", Toast.LENGTH_LONG).show();
                        loadData();
                    }
                });
                dialog.setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
        ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadData();
            }
        });
    }

    private void loadData() {
        String content = etQuery.getText().toString();
        list = new ArrayList<>();
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor;
        String sql = "select * from notepad ";
        if (!"".equals(content)) {
            sql += " where title like ?";
        }
        cursor = db.rawQuery(sql, !"".equals(content) ? new String[]{"%"+content+"%"} : null);
        if (cursor != null && cursor.getColumnCount() > 0) {
            while (cursor.moveToNext()) {
                Integer dbId = cursor.getInt(0);
                String dbTitle = cursor.getString(1);
                String dbContent = cursor.getString(2);
                String dbDate = cursor.getString(3);
                Notepad rubbish = new Notepad(dbId, dbTitle, dbContent, dbDate);
                list.add(rubbish);
            }
        }
        db.close();
        if (list!=null && list.size()>0){
            notepadAdapter.addItem(list);//添加数据到列表
            llEmpty.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }else {
            llEmpty.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    public void add(View view) {
        Intent intent = new Intent(myActivity,AddNotepadActivity.class);
        startActivity(intent);
    }

}